<?php
	/*
	 * This file is part of phpitter.
 	 * 
	 * phpitter is free software: you can redistribute it and/or modify
	 * it under the terms of the GNU General Public License as published by
	 * the Free Software Foundation, either version 3 of the License, or
	 * (at your option) any later version.
 	 * 
	 * phpitter is distributed in the hope that it will be useful,
	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	 * GNU General Public License for more details.
 	 * 
	 * You should have received a copy of the GNU General Public License
	 * along with phpitter.  If not, see <http://www.gnu.org/licenses/>.
	 *
	 * Copyright 2008 Ryan Hadley
	 */

	$gtkNoticeMaxWindows = 4;
	$gtkNoticeWindowTimeOut = 10;
	$gtkNoticeWindowCount = 0;

	$colors['normal'] = '#ffffff';
	$colors['reply'] = '#c7f9c7';
	$colors['direct'] = '#fcfcad';

	function gtkNotice($updates)
	{
		global $gtkNoticeMaxWindows, $gtkNoticeWindowCount;

		$updates = array_reverse($updates);

		if ( count($updates) <= $gtkNoticeMaxWindows )
		{
			foreach($updates as $tweet)
			{
				$summary = $tweet['user']['name'];
				$body = $tweet['text'];
				if ( strlen($body) > 90 )
					$body = substr($body, 0, 87) . '...';
				$tweet['text'] = $body;

				$icon = $tweet['user']['local_image'];

				gtkNoticeSendNoticeSingle($tweet, $icon);
			}
		} else {
			$summary = 'New Tweets';
			$body = 'Received ' . count($updates) . ' updates.';
			gtkNoticeSendNoticeMulti($summary, $body);
		}
	}

	function gtkNoticeSendNoticeMulti($summary, $body)
	{
		global $gtkNoticeWindowTimeOut, $gtkNoticeWindowCount, $colors;

		$newNotice = new GtkWindow();
		$newNotice->set_type_hint(Gdk::WINDOW_TYPE_HINT_NOTIFICATION);
		$newNotice->set_decorated(false);
		$newNotice->set_size_request(400, 80);

		$noticeHTML = new GtkTextView();
		$noticeHTML->set_editable(false);
		$noticeHTML->set_cursor_visible(false);
		$noticeHTML->modify_base(Gtk::STATE_NORMAL, GdkColor::parse($colors['normal']));
		$html = "$summary: $body";
		$noticeHTML->get_buffer()->set_text($html);
		$noticeFrame = new GtkFrame();
		$noticeFrame->add($noticeHTML);
		$newNotice->set_gravity(Gdk::GRAVITY_SOUTH_WEST);
		$newNotice->add($noticeFrame);
		$newNotice->set_keep_above(true);
		$x = $newNotice->get_screen()->get_width() - 305;
		$y = $newNotice->get_screen()->get_height() - 81 - (81 * $gtkNoticeWindowCount);
		$newNotice->move($x, $y);
		$newNotice->show_all();
		Gtk::timeout_add($gtkNoticeWindowTimeOut * 1000, array($newNotice, 'destroy'));
	}

	function gtkNoticeSendNoticeSingle($tweet, $icon)
	{
		global $gtkNoticeWindowTimeOut, $pluggable, $phpTwitter, $gtkNoticeWindowCount, $colors;

		$newNotice = new GtkWindow();
		$newNotice->set_type_hint(Gdk::WINDOW_TYPE_HINT_NOTIFICATION);
		$newNotice->set_decorated(false);
		$newNotice->set_size_request(400, 80);
		
		$uImageBox = GtkImage::new_from_file($icon);
		$uEventBox = new GtkEventBox();
		$uEventBox->add($uImageBox);

		$aTweetBox = new GtkTextView();
		$aTweetBox->set_editable(false);
		$aTweetBox->set_cursor_visible(false);
		$aTweetBox->set_left_margin(5);
		$aTweetBox->set_wrap_mode(Gtk::WRAP_WORD_CHAR);
		$tweetText = $pluggable->displayTweet(html_entity_decode(trim($tweet['text'])));
		$emptyEventBox = new GtkEventBox();

		if ( $tweet['type'] == 'direct_message' ) {
			$aTweetBox->modify_base(Gtk::STATE_NORMAL, GdkColor::parse($colors['direct']));
			$uEventBox->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse($colors['direct']));
			$emptyEventBox->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse($colors['direct']));
			$emptyEventBox2 = new GtkEventBox();
			$emptyEventBox2->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse($colors['direct']));

			$aVBox = new GtkVBox();
			$aVBox->pack_start($uEventBox, false, false);
			$aVBox->pack_start($emptyEventBox);

			$rImageBox = GtkImage::new_from_file($tweet['recipient']['local_image']);
			$rImageBox->set_name($row);
			$rEventBox = new GtkEventBox();
			$rEventBox->add($rImageBox);
			$rEventBox->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse($colors['direct']));

			$rVBox = new GtkVBox();
			$rVBox->pack_start($emptyEventBox2);
			$rVBox->pack_start($rEventBox, false, false);

			$aHBox = new GtkHBox();
			$aHBox->pack_start($aVBox, false, false);
			$aHBox->pack_start($aTweetBox);
			$aHBox->pack_start($rVBox, false, false);
		} else {

			if ( strstr(strtolower($tweet['text']), '@' . strtolower($phpTwitter->username)) )
				$type = 'reply';
			else
				$type = 'normal';

			$aTweetBox->modify_base(Gtk::STATE_NORMAL, GdkColor::parse($colors[$type]));
			$uEventBox->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse($colors[$type]));
			$emptyEventBox->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse($colors[$type]));

			$aVBox = new GtkVBox();
			$aVBox->pack_start($uEventBox, false, false);
			$aVBox->pack_start($emptyEventBox);

			$aHBox = new GtkHBox();
			$aHBox->pack_start($aVBox, false, false);
			$aHBox->pack_start($aTweetBox);
			$tweetText .= "\n" . $tweet['user']['name'] . ' from ' . strip_tags(html_entity_decode($tweet['source'])) . ' ' . getFuzzyTime($tweet['created_at']);
		}

		$aTweetBox->get_buffer()->set_text(html_entity_decode($tweetText));

		linkifyTextView($aTweetBox);
		linkifyTwitterLinks($aTweetBox);

		$noticeFrame = new GtkFrame();
		$noticeFrame->add($aHBox);
		$newNotice->add($noticeFrame);
		$newNotice->set_gravity(Gdk::GRAVITY_SOUTH_WEST);
		$newNotice->set_keep_above(true);
		$x = $newNotice->get_screen()->get_width() - 305;
		$y = $newNotice->get_screen()->get_height() - 81 - (81 * $gtkNoticeWindowCount);
		$newNotice->move($x, $y);
		$newNotice->show_all();
		$gtkNoticeWindowCount++;

		Gtk::timeout_add($gtkNoticeWindowTimeOut * 1000, 'closeNotice', $newNotice);
	}

	function closeNotice($window)
	{
		global $gtkNoticeWindowCount;
		$window->destroy();
		$gtkNoticeWindowCount--;
	}

	$pluggable->registerNewUpdatesCallback('gtkNotice');
?>
